const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');

//获取应用实例
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        showK:true,
        tabActive: 0,
        tabTitles: ['板块排名'],
        hotIndustry: [],
        hotDayType: 1,
        hotIndustrySortDesc:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            showK:app.globalData.k
        })
        // 后端获取数据
        this.getHotIndustry(this.data.hotDayType)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
   
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getTabBar().init();
        let collectStrategyCache = wx.getStorageSync('collectStrategy')
        if (!collectStrategyCache) {
            collectStrategyCache = []
        }
        this.getTabBar().setCartNumber(collectStrategyCache.length)


    },
    onChangeHotDayType(event) {
        console.log(event.detail.index + 1);
        this.getHotIndustry(event.detail.index + 1)
    },

    getHotIndustry(dayType) {
        util.request(api.MarketHotIndustry, { "dayType": dayType }).then(res => {
            if (res.success) {
                this.setData({
                    hotIndustry: res.data.industry,
                    dayType: dayType
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },
    onChangeHotIndustrySort(e){
        let hotIndustry = []
        if(this.data.hotIndustrySortDesc){
            hotIndustry = this.data.hotIndustry.sort((a,b)=>a.zd-b.zd)
        }else{
            hotIndustry = this.data.hotIndustry.sort((a,b)=>b.zd-a.zd)
        }
        this.setData({
            hotIndustrySortDesc:!this.data.hotIndustrySortDesc,
            hotIndustry
        })
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})